-- special_monster
-- create by wangy
-- 杀怪（特殊怪物特殊处理：仙人掌等）

-- 非物理攻击，反射属性等特殊属性只进行魔法攻击或者特殊道具
local specialDamage = function(grid, args)
    -- 是否有优先使用道具
    local classId = args["class_id"];
    if classId and ItemM.getAmount(ME.user, classId) > 0 then
        if DungeonActionM.go("use_property", grid:getPos(), classId) then
            return true;
        end
    end

    -- 优先放主动技
    local slimeSkills = SlimeSkillM.getAllSkills();
    for _, id in pairs(slimeSkills) do
        local pos = grid:getPos();
        --local skillId = SlimeSkillM.query(id, "cast_skill_id");
        local target = SkillM.query(id, "target");
        if target == ATTACKER then
            pos = -1;
        end

        if DungeonActionM.go("use_slime_skill", pos, id) then
            return true;
        end
    end

    -- 获取配置表
    local items = PropertyM.getPropertyByTargetType(PROPERTY_ENEMY);
    -- 试剂需要用其它方式判断
    local reagent = {9001, 9002, 9011, 9021};
    -- 带进迷宫的道具
    local dungeonReagent = ME.user.dbase:query("dungeon_item", {});
    for _, classId in pairs(reagent) do
        -- 从正常伤害道具中剔除
        table.removeItem(items, classId);
        if dungeonReagent[classId] and dungeonReagent[classId] > 0 then
            return DungeonActionM.go("use_property", grid:getPos(), classId);
        end
    end

    for _, classId in pairs(items) do
        if ItemM.getAmount(ME.user, classId) > 0 then
            return DungeonActionM.go("use_property", grid:getPos(), classId);
        end
    end

    -- 用伤害魔法
    local tactics = AutoFightM.getTacticsCfg()["damage"];
    for _, classId in pairs(tactics["args"]) do
        local skillId = SpellM.query(classId, "cast_skill_id");
        -- 提前判断魔法值不足的情况，否则会一直提示
        if SpellM.calcSpellCost(classId) > ME.user:getMp() then
            return false;
        end

        if SpellM.canUseSpell(classId, grid:getPos()) and
            not SkillM.immunitySkill(ME.user, grid.monster, skillId) then
            return DungeonActionM.go("use_spell", grid:getPos(), classId);
        end
    end
    return false;
end

-- 击杀怪物
local fight = function(aliveMonsters, args)
    local pos = -1;
    local baseId = args["base_id"];

    for i = 1, #aliveMonsters do
        local grid = DungeonM.getGridByPos(aliveMonsters[i]);
        local classId = grid.monster:query("base_class_id");
        if baseId and classId and table.indexOf(baseId, classId) >= 0 then
            -- 找到一个相应基类怪物
            pos = aliveMonsters[i];
            break;
        end
    end

    -- 没有找到优先击杀的怪物
    if pos == -1 then
        return false;
    end

    local grid = DungeonM.getGridByPos(pos);

    -- 如果怪物已经死亡了
    if not grid.monster or grid.monster:isDead() then
        return false;
    end

    -- 使用特殊伤害
    if specialDamage(grid, args) then
        return grid:getPos();
    end

end


return {
    -- 判断
    canTrigger = function(args)
        -- 判断是否有存活的怪物
        local aliveMonsters = AutoFightM.getData()["aliveMonsters"];
        if aliveMonsters and #aliveMonsters > 0 then
            return true;
        end

        return false;
    end,

    -- 触发
    trigger = function(args)
        local aliveMonsters = AutoFightM.getData()["aliveMonsters"];

        if not aliveMonsters or #aliveMonsters <= 0 then
            return false;
        end

        -- 击杀怪物
        local ret = fight(aliveMonsters, args);
        if ret then
            -- 抛出事件
            EventMgr.fire(event.PLAYER_MOVE, ret);
            return true;
        end
        return false;
    end,
};
